<template>
	<view class="shopping-cart">
		<view class="nav-title" :style="navTitleStyle">购物车</view>
		<view class="row-1" :style="fixedTop1">
			<view class="tips">商品库存有限，请尽快下单</view>
			<view class="btn" @click="handleState">{{state?'编辑':'完成'}}</view>
		</view>
		<scroll-view scroll-y="true" class="scroll-y row-2" :style="fixedTop2">
			<view>
				<view class="list">
					<view class="list-item" v-for="(item,index) in shoppingCartList" :key="index">
						<view class="icon">
							<checkbox-group @change="selected(item)">
								<checkbox color="orange" style="transform:scale(0.7);" :checked="item.flag"
									class="round" />
							</checkbox-group>
						</view>
						<view class="box">
							<image class="image" :src="realmName + item.goodsPic" mode="aspectFill" />
							<view class="right">
								<view class="title">{{item.goodsName}}</view>
								<view style="display: flex;justify-content: space-between;">
									<view class="price">
										<text style="font-size: 24rpx;margin-bottom: 6rpx;margin-right: 4rpx;">￥</text>
										<text>{{item.mealCardPay || 0}}</text>
									</view>
									<view class="tool">
										<view class="jian">
											<u-icon name="minus" color="#C1C1C1" size="12" @click="reduce(item)">
											</u-icon>
										</view>
										<text class="numb">{{item.num}}</text>
										<view class="jia">
											<u-icon name="plus" color="#FFFFFF" size="12" @click="add(item)"></u-icon>
										</view>
									</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</scroll-view>


		<view class="row-3">
			<view class="icon">
				<checkbox-group @change="selectedall()">
					<checkbox :checked="allchecked" color="orange" style="transform:scale(0.7);" class="round" />
				</checkbox-group>
			</view>
			<!-- <text class="icon-label">全选</text> -->
			<text class="price" v-if="state == 1">总计：￥{{totalPrice}}</text>
			<!-- {{totalNum}} -->
			<view class="submit"  v-if="state == 1" @click="handleCart(totalNum)" :style="totalNum?'':'background-color:#C1C1C1;color: #FFFFFF;'">结算</view>
			<view class="delete " :class="delDisabled ? 'delDisabled' : ''" v-if="state == 0" @click="handleDel()">删除
			</view>
		</view>
	</view>
</template>

<script>
	import {
		wcShoppingCartEdit,
		wcShoppingCartDel,
		wcShoppingCartList,
		addressList,
		realmName
	} from "@/http";
	export default {
		data() {
			return {
				val: false,
				realmName,
				state: 1, //1编辑模式 0选择商品模式
				shoppingCartList: [],
				addressList:[],
				userInfo: uni.getStorageSync('userInfo'),
				allchecked: false,
				disabled: true,
				submitPrice: '',
				delDisabled: true,
			}
		},
		watch: {
			// 监听结算的订单数，当订单数小于0的时候，结算按钮为禁用状态，用户不能提交订单
			totalNum(val) {
				if (val != 0) {
		 		this.disabled = false
				} else {
					this.disabled = true
				}
			},
			// 监听totalprice的值，再将值赋给submitPrice，提交订单的时候传的参数是submitPrice
			totalPrice(val) {
				this.submitPrice = val
		 }
		},
		methods: {
			async init() {
				this.wcShoppingCartList();
				let res = await addressList({
					userPhone: this.userInfo.phone
				});
				this.addressList = res.data.rows;
			},
			handleDailyBestNum(data) {
				this.shoppingCartList = data;
			},
			// 减号操作
			reduce(item) {
				var id = item.id
				if (item.num > 1) {
					var myNum = --item.num
					this.wcShoppingCartEdit(myNum, id)
				} else {
					item.num = 1
					var myNum = 1
					this.wcShoppingCartEdit(myNum, id)
					return
				}
			},
			// 加号操作
			add(item) {
				var id = item.id
				var goodsPrice = item.myPrice
				var myNum = ++item.num
				this.wcShoppingCartEdit(myNum, id)
			},
			async wcShoppingCartEdit(myNum, id) {
				let res = await wcShoppingCartEdit({
					num: myNum,
					id: id
				})
			},
			handleDel() {
				if (this.delDisabled) return false
				let ids = []
				this.shoppingCartList.filter(item => {
					if (item.flag == true) {
						ids.push(item.id)
					}
				})
				let idsJoin = ids.join(',')
				this.wcShoppingCartDel(idsJoin);
				
			},
			async wcShoppingCartDel(idsJoin) {
				let res = await wcShoppingCartDel(idsJoin);
				this.wcShoppingCartList()
			},
			async wcShoppingCartList() {
				let res = await wcShoppingCartList({
					buyerPhone: this.userInfo.phone
				});
				this.shoppingCartList = res.data.rows.map(item => {
					item.flag = false;
					return item;
				});
				if(!this.shoppingCartList.length) this.allchecked = false;			
			},
			// 单个商品前的勾选
			selected(item) {
				// 反选
				item.flag = !item.flag;
				this.isDel();
				if (!item.flag) {
					this.allchecked = false;
				} else {
					const test = this.shoppingCartList.every(item => {
						return item.flag === true
					})
					if (test) {
						this.allchecked = true
					} else {
						this.allchecked = false
					}
				}
			},
			// 全选按钮
			selectedall() {
				this.allchecked = !this.allchecked
				if (this.allchecked) {
					this.delDisabled = false;
			 	this.shoppingCartList.map(item => {
						item.flag = true
					})
				} else {
					this.delDisabled = true;
					this.shoppingCartList.map(item => {
						item.flag = false
					})
				}
			},
			handleCart(totalNum) {
				if(!totalNum) return;
				uni.setStorageSync('PAGE_DATA', {
					address: this.addressList[0],
					list: this.shoppingCartList.filter(item=> item.flag)
				});
				this.allchecked = false;
				uni.navigateTo({
					url: "/culturalCreativity/common/sureOrder"
				});
			},
			handleState() {
				this.state = this.state ? 0 : 1;
				this.isDel();
			},
			isDel() {
				if (this.state == 0) {
					const test = this.shoppingCartList.some(item => {
						return item.flag === true
					})
					if (test) {
						this.delDisabled = false
					} else {
						this.delDisabled = true
					}
				}
			}
		},
		computed: {
			// 计算选中商品数量
			totalNum() {
				let totalNum = 0;
				this.shoppingCartList.map(item => {
					item.flag ? totalNum += 1 : totalNum += 0
				})
				return totalNum
			},
			//计算选中商品的总价
			totalPrice() {
				let totalPrice = 0;
				this.shoppingCartList.map(item => {
					item.flag ? totalPrice += item.num * item.mealCardPay : totalPrice += 0
				})
				return totalPrice.toFixed(2);
			},
			navTitleStyle() {
				let res = wx.getMenuButtonBoundingClientRect();
				return `padding-top:${res.top}px;height:${res.height}px;line-height:${res.height}px;`
			},
			fixedTop1() {
				let res = wx.getMenuButtonBoundingClientRect();
				return `top:${res.top + res.height}px;`
			},
			fixedTop2() {
				let res = wx.getMenuButtonBoundingClientRect();
				return `top:${res.top + res.height + 40}px;`
			}
		}
	}
</script>
<style lang="scss" scoped>
	.shopping-cart {
		.row-3 {
			position: fixed;
			z-index: 2;
			left: 0;
			right: 0;
			bottom: calc(50px + env(safe-area-inset-bottom));
			height: 110rpx;
			background-color: #FFFFFF;
			padding: 0 32rpx;
			box-sizing: border-box;
			display: flex;
			align-items: center;

			.delete {
				position: absolute;
				right: 32rpx;
				width: 186rpx;
				height: 76rpx;
				border-radius: 40rpx;
				border: 2rpx solid #BDBDBD;
				line-height: 76rpx;
				text-align: center;
				font-size: 32rpx;
				font-weight: 400;
				color: #FD8F19;
			}

			.delete.delDisabled {
				color: #BDBDBD;
			}

			.submit {
				position: absolute;
				right: 32rpx;
				width: 190rpx;
				height: 80rpx;
				background: #FD8F19;
				border-radius: 40rpx;
				text-align: center;
				line-height: 80rpx;
				font-size: 32rpx;
				font-weight: 400;
				color: #FFFFFF;
			}

			.submit.disabled {
				background-color: #C1C1C1;
			}

			.icon {
				width: 44rpx;
				height: 44rpx;
				margin-right: 16rpx;
			}

			.icon-label {
				font-size: 28rpx;
				font-weight: 400;
				color: #1F1F1F;
			}

			.price {
				margin-left: 32rpx;
				font-size: 28rpx;
				font-weight: 400;
				color: #FD8F19;
			}
		}

		.scroll-y {
			position: fixed;
			left: 0;
			right: 0;
			bottom: calc(50px + env(safe-area-inset-bottom) + 110rpx);
			z-index: 0;

			.list {
				padding: 0 32rpx;
				padding-bottom: 100rpx;

				.list-item {
					margin-top: 24rpx;
					display: flex;
					justify-content: space-between;
					align-items: center;

					.icon {
						width: 44rpx;
						height: 44rpx;
						margin-right: 24rpx;
					}

					.box {
						flex: 1;
						height: 256rpx;
						background: #FFFFFF;
						border-radius: 30rpx;
						box-sizing: border-box;
						padding: 24rpx;
						display: flex;

						.image {
							min-width: 208rpx;
							max-width: 208rpx;
							height: 208rpx;
							border-radius: 16rpx;
							margin-right: 24rpx;
						}

						.right {
							display: flex;
							flex-direction: column;
							justify-content: space-between;
							flex: 1;
							.title {
								width: 100%;
								display: -webkit-box;
								overflow: hidden;
								white-space: normal !important;
								text-overflow: ellipsis;
								word-wrap: break-word;
								-webkit-line-clamp: 2;
								-webkit-box-orient: vertical;
							}

							.price {
								font-size: 44rpx;
								font-weight: 500;
								color: #FD8F19;
								display: flex;
								align-items: flex-end;
								width: 160rpx;
							}

							.tool {
								display: flex;
								align-items: center;

								.jian,
								.jia {
									display: flex;
									justify-content: center;
									align-items: center;
									width: 44rpx;
									height: 44rpx;
									border-radius: 16rpx;
									border: 2rpx solid #BDBDBD;
									background-color: #FFFFFF;
								}

								.numb {
									width: 60rpx;
									text-align: center;
									font-size: 32rpx;
									font-weight: 400;
									color: #1F1F1F;
								}

								.jia {
									border-color: #FD8F19;
									background-color: #FD8F19;
								}
							}
						}
					}
				}
			}
		}

		.row-1 {
			position: fixed;
			top: 125rpx;
			left: 0;
			right: 0;
			z-index: 1;
			padding: 0 32rpx;
			height: 80rpx;
			background-color: #FFFFFF;
			font-size: 24rpx;
			font-weight: 400;
			display: flex;
			justify-content: space-between;
			align-items: center;

			.tips {
				color: #878787;
			}

			.btn {
				color: #1F1F1F;
			}
		}

		.nav-title {
			position: fixed;
			z-index: 1;
			left: 0;
			right: 0;
			text-align: center;
			font-weight: 500;
			color: rgba(31, 31, 31, 1);
			top: 0;
			padding-bottom: 12rpx;
			font-size: 36rpx;
			background: #FFFFFF;
		}

		&::after {
			content: "";
			position: fixed;
			top: 0;
			right: 0;
			bottom: 0;
			left: 0;
			z-index: -1;
			background-color: #F7F7F7;
		}
	}
</style>
